package com.bawei.detail

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.detail.adapter.CommendListAdapter
import com.bawei.detail.databinding.FragmentCommendBinding
import com.bawei.detail.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class CommendFragment : BaseMviFragment<FragmentCommendBinding>() {

    private val mViewModel: DetailViewModel by activityViewModels()
    private lateinit var mRlvCommends: CommendExpandListView
    private lateinit var mCommendAdapter: CommendListAdapter
    private var commendList = mutableListOf<CommendEntity>()
    private var locReplayList = mutableListOf<ReplyCommendEntity>()

    private var itemId: String = ""

    companion object {
        @JvmStatic
        fun newInstance(itemId: String) =
            CommendFragment().apply {
                arguments = Bundle().apply {
                    putString("itemId", itemId)
                }
            }
    }

    override fun getLayoutId(): Int = R.layout.fragment_commend

    override fun initViews() {
        arguments?.let {
            itemId = it.getString("itemId").toString()
        }

        mRlvCommends = mDataBinding.elvComments
        mCommendAdapter = CommendListAdapter(commendList)
        mRlvCommends.setAdapter(mCommendAdapter)

        mockReplyCommentData()
    }

    private fun mockReplyCommentData(){
        locReplayList.add(ReplyCommendEntity(0, 0, "我来了", "2023-12-20 14:23:21", 0, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "假的真不了", "2023-12-20 14:23:23", 1, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "言之有理", "2023-12-20 14:23:25", 2, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 3, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "啥也不是！！", "2023-12-20 14:23:26", 4, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "太傻了！！", "2023-12-20 14:23:26", 5, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 6, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "给你点赞！！", "2023-12-20 14:23:26", 7, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "努力加油！！", "2023-12-20 14:23:26", 8, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 9, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "加油！！", "2023-12-20 14:23:26", 10, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "一点儿不真实！！", "2023-12-20 14:23:26", 11, "小西", 0, 1, "" ))
        locReplayList.add(ReplyCommendEntity(0, 0, "牛逼牛逼，太漂亮了！！", "2023-12-20 14:23:26", 12, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(DetailViewIntent.LoadMovieCommends(1, itemId))
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is DetailUiState.LoadMovieCommendSuccess -> updateCommendList(it.list)
                is DetailUiState.LoadMovieCommendFail -> showToastMessage(it.msg)
                else -> {}
            }
        }
    }

    private fun updateCommendList(list: List<CommendEntity>) {
        val locCommendSize = locReplayList.size
        list.forEachIndexed { index, commendEntity ->
            commendEntity.userlogo = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/f4217ff67c16a8daf70e423cccc602fb~300x300.image"
            commendEntity.replytotal = 3
            //添加第一条
            val fromIndex1 = (locCommendSize - 2) % ((Math.random() * 10).toInt() + 1)
            commendEntity.replyList.addAll(locReplayList.subList(fromIndex1, fromIndex1 + 1))
            //添加第二条
            val fromIndex2 = (locCommendSize - 2) % ((Math.random() * 10).toInt() + 1)
            commendEntity.replyList.addAll(locReplayList.subList(fromIndex2, fromIndex2 + 1))
            //添加第三条
            val fromIndex3 = (locCommendSize - 2) % ((Math.random() * 10).toInt() + 1)
            commendEntity.replyList.addAll(locReplayList.subList(fromIndex3, fromIndex3 + 1))
        }
        commendList.addAll(list)
        mCommendAdapter.notifyDataSetChanged()
    }


}